/**
 * 
 */
package com.module.bp.ui;

import java.awt.Color;

import com.dalonedrau.jogl.engine.Action;
import com.dalonedrau.jogl.engine.GameApplet;
import com.dalonedrau.jogl.engine.ProjectConstants;
import com.dalonedrau.jogl.opengl.sprite.BasicSpriteObject;
import com.dalonedrau.jogl.opengl.sprite.SpriteBase;
import com.dalonedrau.jogl.opengl.sprite.SpriteImageObject;
import com.dalonedrau.jogl.opengl.sprite.SpriteImageObjectFactory;
import com.dalonedrau.jogl.opengl.ui.Button;
import com.dalonedrau.jogl.opengl.ui.GuiComponent;
import com.dalonedrau.jogl.opengl.ui.Label;
import com.dalonedrau.jogl.opengl.ui.UIFactory;
import com.dalonedrau.jogl.opengl.ui.border.EmptyBorder;
import com.dalonedrau.jogl.opengl.ui.border.VXBorder2;
import com.dalonedrau.jogl.opengl.ui.panel.LandscapePagePanel;
import com.dalonedrau.jogl.opengl.ui.panel.Panel;
import com.dalonedrau.pool.PooledStringBuilder;
import com.dalonedrau.pool.StringBuilderPool;
import com.dalonedrau.watchable.Watchable;
import com.dalonedrau.watchable.Watcher;
import com.module.bp.player.BPParty;

/**
 * @author Donald
 */
public class PartyStatsBar extends LandscapePagePanel implements Watcher {
	/** the singleton instance of {@link PartyStatsBar}. */
	private static PartyStatsBar	instance;
	/**
	 * Gets the singleton instance of {@link PartyStatsBar}.
	 * @return {@link PartyStatsBar}
	 */
	public static PartyStatsBar getInstance() {
		if (PartyStatsBar.instance == null) {
			PartyStatsBar.instance = new PartyStatsBar();
		}
		return PartyStatsBar.instance;
	}
	private Button	btn;
	/** the label displaying the party's total gold. */
	private Label	lblGold;
	/** the label displaying the party's total payroll. */
	private Label	lblPayroll;
	/** the panel for displaying gold and payroll. */
	private Panel	pnlPay;
	private boolean	slideOut	= true;
	/**
	 * Creates a new instance of {@link PartyStatsBar}.
	 */
	private PartyStatsBar() {
		super(Panel.LANDSCAPE_CENTER);
		super.setBorder(new VXBorder2());
		try {
			btn = UIFactory.getInstance().getStandardButton("");
			SpriteBase sb = new BasicSpriteObject();
			SpriteImageObject img =
					SpriteImageObjectFactory.getInstance().getImageByName(
							"arr_lt");
			btn.addAction(new Action() {
				/**
				 * {@inheritDoc}
				 */
				@Override
				public void performAction() {
					try {
						if (slideOut) {
							btn.getSprite().setImageRefId(
									SpriteImageObjectFactory.getInstance()
											.getImageRefId("arr_rt"));
						} else {
							btn.getSprite().setImageRefId(
									SpriteImageObjectFactory.getInstance()
											.getImageRefId("arr_lt"));
						}
						slideOut = !slideOut;
					} catch (Exception ex) {
						JOGLErrorHandler.getInstance().fatalError(ex);
					}
				}
			});
			sb.setImageRefId(img.getRefId());
			sb.setWidth(img.getWidth());
			sb.setHeight(img.getHeight());
			btn.setSprite(sb);
			sb = null;
			super.addChild(btn);
			pnlPay = initGoldPanel();
			super.addChild(pnlPay);
			pnlFood = initFoodPanel();
			super.addChild(pnlFood);
			BPParty.getInstance().addWatcher(this);
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}
	/** the panel for displaying food and food obligations. */
	private Panel pnlFood;	
	/** the label displaying the party's total food. */
	private Label			lblFood;
	/** the label displaying the total weight the party is allowed to carry. */
	private Label			lblWeightAllowed;
	/** the label displaying the total weight the party is carrying. */
	private Label			lblWeightCarrying;
	/** the label displaying the party's total feedroll. */
	private Label			lblFeedroll;
	/**
	 * Gets the panel displaying the party's food.
	 * @return {@link Panel}
	 * @throws Exception if an error occurs
	 */
	private Panel initFoodPanel() throws Exception {
		Panel pnl = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
		SpriteBase sb = new BasicSpriteObject();
		SpriteImageObject img =
				SpriteImageObjectFactory.getInstance().getImageByName(
						"symbol_food");
		sb.setImageRefId(img.getRefId());
		sb.setWidth(img.getWidth());
		sb.setHeight(img.getHeight());
		Label lblIcon = new Label(sb);
		lblIcon.setBorder(new EmptyBorder(0, 0, 0, 4));
		pnl.addChild(lblIcon);
		lblFood =
				new Label(ProjectConstants.getInstance().getFontH1(), "  0");
		lblFood.setHorizontalAlignment(GuiComponent.LEFT);
		pnl.addChild(lblFood);
		pnl.addChild(
				new Label(ProjectConstants.getInstance().getFontH1(), "/"));
		lblFeedroll =
				new Label(ProjectConstants.getInstance().getFontH1(), "0");
		pnl.addChild(lblFeedroll);
		lblFeedroll.setHorizontalAlignment(GuiComponent.RIGHT);
		pnl.setTooltipText("");
		return pnl;
	}
	/**
	 * Gets the panel displaying the party's gold.
	 * @return {@link Panel}
	 * @throws Exception if an error occurs
	 */
	private Panel initGoldPanel() throws Exception {
		Panel pnl = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
		SpriteBase sb = new BasicSpriteObject();
		SpriteImageObject img =
				SpriteImageObjectFactory.getInstance().getImageByName(
						"symbol_gold");
		sb.setImageRefId(img.getRefId());
		sb.setWidth(img.getWidth());
		sb.setHeight(img.getHeight());
		Label lblIcon = new Label(sb);
		lblIcon.setBorder(new EmptyBorder(0, 0, 0, 4));
		pnl.addChild(lblIcon);
		lblGold =
				new Label(ProjectConstants.getInstance().getFontH1(), "  0");
		lblGold.setHorizontalAlignment(GuiComponent.LEFT);
		pnl.addChild(lblGold);
		pnl.addChild(
				new Label(ProjectConstants.getInstance().getFontH1(), "/"));
		lblPayroll =
				new Label(ProjectConstants.getInstance().getFontH1(), "0");
		lblPayroll.setDebug(true);
		lblPayroll.setHorizontalAlignment(GuiComponent.RIGHT);
		pnl.addChild(lblPayroll);
		return pnl;
	}
	/** Update stats about the party's gold and payroll. */
	private void updatePayStats() {
		long g = BPParty.getInstance().getPartyGold();
		int p = BPParty.getInstance().getPartyPayroll();
		// set gold
		PooledStringBuilder sb =
				StringBuilderPool.getInstance().getStringBuilder();
		sb.append(g);
		lblGold.setText(sb.toString());
		sb.setLength(0);
		if (g < p) {
			lblGold.setColor(Color.RED);
			sb.append("You have ");
			sb.append(g);
			sb.append(" gold available, but you must have ");
			sb.append(p);
			sb.append(" gold to pay your followers by the ");
			sb.append("end of the day, or they will begin to abandon you.");
			pnlPay.setTooltipText(sb.toString());
		} else {
			lblGold.setColor(Color.WHITE);
			sb.append("You have ");
			sb.append(g);
			if (p > 0) {
				sb.append(" gold available, enough to pay ");
				sb.append("your warband for the day.");
			} else {
				sb.append(" gold available.");
			}
			pnlPay.setTooltipText(sb.toString());
		}
		sb.setLength(0);
		// set payroll
		sb.append(p);
		lblPayroll.setText(sb.toString());
		sb.setLength(0);
		sb.returnToPool();
	}
	/** Update stats about the party's food. */
	private void updateFoodStats() {
		int f = BPParty.getInstance().getPartyFood();
		int o = BPParty.getInstance().getPartyFoodObligation();
		PooledStringBuilder sb =
				StringBuilderPool.getInstance().getStringBuilder();
		sb.append(f);
		lblFood.setText(sb.toString());
		sb.setLength(0);
		if (f < o) {
			lblFood.setColor(Color.RED);
			sb.append("You have ");
			sb.append(f);
			sb.append(" food rations available, but you must have at least ");
			sb.append(o);
			sb.append(" food ");
			if (o == 1) {
				sb.append(" ration ");
			} else {
				sb.append(" rations ");
			}
			sb.append(" to feed your warband by the ");
			sb.append("end of the day, or you will begin to starve.");
			this.pnlFood.setTooltipText(sb.toString());
		} else {
			lblFood.setColor(Color.WHITE);
			sb.append("You have ");
			sb.append(f);
			sb.append(" food rations available, enough to feed ");
			sb.append("your warband for the day.");
			this.pnlFood.setTooltipText(sb.toString());
		}
		sb.setLength(0);
		// set foodroll
		sb.append(o);
		lblFeedroll.setText(sb.toString());
		sb.setLength(0);
		sb.returnToPool();
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void watchUpdated(final Watchable data) {
		try {
			updatePayStats();
			updateFoodStats();
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}

}
